// 列表初始化
import { ref } from 'vue'
import { getWhiteListApi } from '@/api/whiteList/management'
import { deepClone } from '@/util'
import globalData from '@/staticData/globalData'

const resetForm = () => {
  return {
    pageIndex: 1,
    pageSize: 20
  }
}
const searchForm = ref(resetForm())
const formItems = ref([
  {
    field: 'signerName',
    type: 'auto-complete',
    props: { label: '签署人姓名' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 16
    }
  },
  {
    field: 'signerType',
    type: 'select',
    props: { label: '签署人证件类型' },
    attr: {
      placeholder: '请选择内容',
      multiple: false
    },
    options: globalData.SIGNER_TYPE_LIST
  },
  {
    field: 'signerNumber',
    type: 'auto-complete',
    props: { label: '签署人证件号' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 32,
      validateText: '仅支持输入中文，英文字母'
    },
    validateRegex: /^[\u4e00-\u9fa5a-zA-Z0-9]+$/
  },
  {
    field: 'modifiedDatetime',
    type: 'date-picker',
    props: { label: '更新时间' },
    attr: {
      placeholder: '请选择内容',
      type: 'daterange'
    }
  }
])
const columns = ref([
  {
    label: '姓名',
    prop: 'name'
  },
  {
    label: '证件类型',
    prop: 'typeDesc'
  },
  {
    label: '证件号',
    prop: 'number'
  },
  {
    label: '添加/修改原因',
    prop: 'reason'
  },
  {
    label: '更新时间',
    prop: 'modifiedDatetime',
    sortable: 'custom'
  },
  {
    label: '操作',
    prop: 'gender',
    slots: { customRender: 'action' }
  }
])
const businessLineList = ref([]) // 业务线数据
const total = ref(0)
const schemaTableRef = ref(null)

export default function useList() {
  // 查询数据
  const loadData = async val => {
    if (Object.keys(searchForm.value).length === 0) {
      searchForm.value = resetForm()
    }
    let params = {}
    params = deepClone(searchForm.value)
    console.log('查询参数', params)
    // 处理修改时间
    if (params.modifiedDatetime?.length > 0) {
      params.startModifyTime = `${params.modifiedDatetime[0]} 00:00:00`
      params.endModifyTime = `${params.modifiedDatetime[1]} 23:59:59`
      delete params.modifiedDatetime
    }
    // 处理数据排序
    if (val.order) {
      val.order === 'asc' ? params.modifyTimeSort = 1 : params.modifyTimeSort = 0
    }
    return getWhiteListApi(params).then(res => {
      total.value = res.total
      console.log('查询数据', res)
      return {
        rows: res.data ? res.data : [] || []
      }
    })
  }

  // 搜索
  const search = async() => {
    if (schemaTableRef.value.localLoading) return
    schemaTableRef.value.refresh(true)
  }

  // 分页
  const handleSizeChange = async e => {
    searchForm.value.pageSize = e
    searchForm.value.pageIndex = 1
    search()
  }

  return {
    searchForm,
    formItems,
    columns,
    businessLineList,
    total,
    schemaTableRef,
    loadData,
    search,
    handleSizeChange
  }
}
